<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>微信雷霆战机</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            touch-action: none;
        }
        #gameCanvas {
            background: #000;
        }
        #score {
            position: fixed;
            top: 10px;
            left: 10px;
            color: white;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="score">得分: 0</div>
    <canvas id="gameCanvas"></canvas>

<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');

// 设置画布尺寸
function resizeCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);

// 游戏对象
const game = {
    player: {
        x: canvas.width/2 - 25,
        y: canvas.height - 100,
        width: 50,
        height: 50,
        speed: 8
    },
    bullets: [],
    enemies: [],
    score: 0,
    isGameOver: false
};

// 玩家飞机控制
let touchX = canvas.width/2 - 25;
canvas.addEventListener('touchmove', (e) => {
    e.preventDefault();
    touchX = e.touches[0].clientX - game.player.width/2;
});

// 游戏元素
function drawPlayer() {
    ctx.fillStyle = '#00f';
    ctx.fillRect(game.player.x, game.player.y, game.player.width, game.player.height);
}

function drawBullets() {
    ctx.fillStyle = '#ff0';
    game.bullets.forEach(bullet => {
        ctx.fillRect(bullet.x, bullet.y, 10, 20);
    });
}

function drawEnemies() {
    ctx.fillStyle = '#f00';
    game.enemies.forEach(enemy => {
        ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
    });
}

// 射击逻辑
function shoot() {
    game.bullets.push({
        x: game.player.x + game.player.width/2 - 5,
        y: game.player.y,
        width: 10,
        height: 20,
        speed: 10
    });
}

// 生成敌机
function spawnEnemy() {
    game.enemies.push({
        x: Math.random() * (canvas.width - 40),
        y: -40,
        width: 40,
        height: 40,
        speed: 3 + Math.random() * 2
    });
}

// 碰撞检测
function checkCollision(rect1, rect2) {
    return rect1.x < rect2.x + rect2.width &&
           rect1.x + rect1.width > rect2.x &&
           rect1.y < rect2.y + rect2.height &&
           rect1.y + rect1.height > rect2.y;
}

// 游戏循环
function gameLoop() {
    if(game.isGameOver) return;

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    // 更新玩家位置
    game.player.x = touchX;
    game.player.x = Math.max(0, Math.min(canvas.width - game.player.width, game.player.x));
    
    // 自动射击
    if(Date.now() % 200 < 16) {
        shoot();
    }
    
    // 更新子弹
    game.bullets = game.bullets.filter(bullet => {
        bullet.y -= bullet.speed;
        return bullet.y > -20;
    });
    
    // 更新敌机
    if(Date.now() % 100 < 16) {
        spawnEnemy();
    }
    
    game.enemies = game.enemies.filter(enemy => {
        enemy.y += enemy.speed;
        
        // 检测敌机与子弹碰撞
        game.bullets.forEach(bullet => {
            if(checkCollision(bullet, enemy)) {
                game.score += 100;
                scoreElement.textContent = `得分: ${game.score}`;
                enemy.dead = true;
            }
        });
        
        // 检测玩家碰撞
        if(checkCollision(enemy, game.player)) {
            gameOver();
        }
        
        return !enemy.dead && enemy.y < canvas.height;
    });
    
    // 绘制游戏元素
    drawPlayer();
    drawBullets();
    drawEnemies();
    
    requestAnimationFrame(gameLoop);
}

function gameOver() {
    game.isGameOver = true;
    ctx.fillStyle = 'rgba(0,0,0,0.7)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = '#fff';
    ctx.font = '40px Arial';
    ctx.textAlign = 'center';
    ctx.fillText('游戏结束', canvas.width/2, canvas.height/2);
    ctx.font = '30px Arial';
    ctx.fillText(`最终得分: ${game.score}`, canvas.width/2, canvas.height/2 + 50);
}

// 开始游戏
gameLoop();
</script>
</body>
</html>